<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>频道管理 - Shioa</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#eff6ff',
                            100: '#dbeafe', 
                            200: '#bfdbfe',
                            300: '#93c5fd',
                            400: '#60a5fa',
                            500: '#3b82f6',
                            600: '#2563eb',
                            700: '#1d4ed8',
                            800: '#1e40af',
                            900: '#1e3a8a'
                        },
                        secondary: '#64748b',
                        accent: {
                            green: '#10b981',
                            purple: '#8b5cf6',
                            orange: '#f59e0b',
                            red: '#ef4444',
                            blue: '#06b6d4'
                        },
                        success: '#10b981',
                        warning: '#f59e0b',
                        danger: '#ef4444',
                        info: '#06b6d4'
                    },
                    borderRadius: {
                        'button': '8px'
                    },
                    fontFamily: {
                        'sans': ['Inter', 'ui-sans-serif', 'system-ui']
                    },
                    boxShadow: {
                        'soft': '0 2px 15px -3px rgba(0, 0, 0, 0.07), 0 10px 20px -2px rgba(0, 0, 0, 0.04)',
                        'medium': '0 4px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 20px -5px rgba(0, 0, 0, 0.04)',
                        'strong': '0 10px 40px -10px rgba(0, 0, 0, 0.1), 0 2px 10px -2px rgba(0, 0, 0, 0.04)'
                    }
                }
            }
        }
    </script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            min-height: 1024px;
        }
        .toast-container { 
            position: fixed; 
            top: 1rem; 
            right: 1rem; 
            z-index: 9999; 
        }
        .status-bar { 
            position: fixed; 
            top: 0; 
            left: 0; 
            right: 0; 
            z-index: 50; 
        }
        
        /* 表格滚动条美化 */
        .table-container {
            scrollbar-width: thin;
            scrollbar-color: #cbd5e1 #f1f5f9;
        }
        .table-container::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }
        .table-container::-webkit-scrollbar-track {
            background: #f1f5f9;
            border-radius: 4px;
        }
        .table-container::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 4px;
        }
        .table-container::-webkit-scrollbar-thumb:hover {
            background: #94a3b8;
        }
        
        /* 加载状态 */
        .loading-overlay {
            background: rgba(255, 255, 255, 0.8);
            backdrop-filter: blur(4px);
        }
    </style>
</head>
<body class="bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-50 min-h-screen font-sans">
    <!-- 状态提示栏 -->
    <div id="status-bar" class="hidden status-bar bg-blue-500 text-white p-3 text-center">
        <div class="flex items-center justify-center">
            <div class="animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"></div>
            <span id="status-text">正在加载...</span>
        </div>
    </div>

    <!-- Toast 容器 -->
    <div id="toast-container" class="toast-container space-y-2"></div>

    <!-- 导航由 shared-navigation.js 统一管理，实现真正的组件复用 -->

    <!-- 面包屑导航 -->
    <div class="max-w-[1440px] mx-auto px-6 py-4">
        <nav class="flex items-center gap-2 text-sm text-gray-500">
            <a href="index.html" class="hover:text-primary-600 transition-colors">
                <i class="fas fa-home"></i>
            </a>
            <span class="mx-2">/</span>
            <span class="text-gray-700">频道管理</span>
        </nav>
    </div>

    <!-- 主内容区域 -->
    <main class="max-w-[1440px] mx-auto px-6 pb-8">
        <!-- 标题已移除 -->

        <!-- 频道列表 -->
        <div class="bg-white rounded-xl shadow-medium border border-gray-100 overflow-hidden">
            <div class="p-6 border-b border-gray-100 bg-gradient-to-r from-gray-50 to-white">
                <!-- 合并为一行的布局 -->
                <div class="flex items-center justify-between gap-6">
                    <!-- 左侧：标题和搜索 -->
                    <div class="flex items-center gap-6 flex-1">
                        <div class="flex items-center gap-3">
                            <div class="w-8 h-8 bg-gradient-to-br from-indigo-400 to-indigo-600 rounded-lg flex items-center justify-center">
                                <i class="fas fa-list text-white text-sm"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-bold text-gray-800">频道列表</h3>
                                <p class="text-sm text-gray-500">共 <span id="total-channels">0</span> 个频道</p>
                            </div>
                        </div>
                        
                        <!-- 搜索框 -->
                        <div class="flex-1 max-w-md">
                            <div class="relative">
                                <input type="text" id="search-channels" placeholder="搜索频道名称或ID..." 
                                       class="w-full pl-10 pr-4 py-2.5 border border-gray-300 rounded-button focus:border-primary-500 focus:ring-2 focus:ring-primary-200 focus:outline-none transition-all">
                                <i class="fas fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 右侧：筛选器和操作按钮 -->
                    <div class="flex items-center gap-3">
                        <select id="filter-category" class="px-3 py-2.5 border border-gray-300 rounded-button focus:border-primary-500 focus:ring-2 focus:ring-primary-200 focus:outline-none">
                            <option value="">所有分类</option>
                            <option value="科技">科技</option>
                            <option value="生活">生活</option>
                            <option value="娱乐">娱乐</option>
                            <option value="游戏">游戏</option>
                            <option value="教育">教育</option>
                            <option value="音乐">音乐</option>
                            <option value="美食">美食</option>
                            <option value="旅行">旅行</option>
                            <option value="其他">其他</option>
                        </select>
                        
                        <select id="filter-status" class="px-3 py-2.5 border border-gray-300 rounded-button focus:border-primary-500 focus:ring-2 focus:ring-primary-200 focus:outline-none">
                            <option value="">所有状态</option>
                            <option value="active">活跃</option>
                            <option value="inactive">停用</option>
                        </select>
                        
                        <button onclick="clearFilters()" class="px-3 py-2.5 text-gray-600 hover:text-gray-800 border border-gray-300 rounded-button hover:bg-gray-50 transition-colors" title="清除筛选">
                            <i class="fas fa-times"></i>
                        </button>
                        
                        <button onclick="openChannelFilterModal()" class="flex items-center gap-2 bg-gradient-to-r from-purple-500 to-purple-600 text-white px-4 py-2.5 rounded-button font-medium shadow-lg hover:from-purple-600 hover:to-purple-700 hover:shadow-xl transform hover:-translate-y-0.5 transition-all duration-200" title="高级筛选">
                            <i class="fas fa-filter text-sm"></i>
                            <span class="hidden sm:inline">高级筛选</span>
                        </button>
                        
                        <button onclick="openAddChannelModal()" class="flex items-center gap-2 bg-gradient-to-r from-primary-500 to-primary-600 text-white px-4 py-2.5 rounded-button font-medium shadow-lg hover:from-primary-600 hover:to-primary-700 hover:shadow-xl transform hover:-translate-y-0.5 transition-all duration-200">
                            <i class="fas fa-plus text-sm"></i>
                            <span class="hidden sm:inline">添加频道</span>
                        </button>
                    </div>
                </div>
            </div>
            
            <!-- 频道统计概览 -->
            <div class="p-6 border-b border-gray-100 bg-gray-50/50">
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                    <div class="text-center p-4 bg-white rounded-lg border">
                        <div class="text-2xl font-bold text-primary-600" id="stat-total-channels">0</div>
                        <div class="text-sm text-gray-600">总频道数</div>
                    </div>
                    <div class="text-center p-4 bg-white rounded-lg border">
                        <div class="text-2xl font-bold text-green-600" id="stat-active-channels">0</div>
                        <div class="text-sm text-gray-600">活跃频道</div>
                    </div>
                    <div class="text-center p-4 bg-white rounded-lg border">
                        <div class="text-2xl font-bold text-blue-600" id="stat-total-subscribers">0</div>
                        <div class="text-sm text-gray-600">总订阅数</div>
                    </div>
                    <div class="text-center p-4 bg-white rounded-lg border">
                        <div class="text-2xl font-bold text-purple-600" id="stat-total-videos">0</div>
                        <div class="text-sm text-gray-600">总视频数</div>
                    </div>
                </div>
            </div>
            
            <!-- 频道表格 -->
            <div class="table-container overflow-x-auto">
                <table class="w-full" id="channels-table">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                <input type="checkbox" id="select-all-channels" class="rounded border-gray-300 text-primary-600 focus:ring-primary-500">
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700" onclick="sortChannels('channel_name')">
                                频道名称 <i class="fas fa-sort ml-1"></i>
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700" onclick="sortChannels('category')">
                                分类 <i class="fas fa-sort ml-1"></i>
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700" onclick="sortChannels('subscriber_count')">
                                订阅数 <i class="fas fa-sort ml-1"></i>
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700" onclick="sortChannels('video_count')">
                                视频数 <i class="fas fa-sort ml-1"></i>
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider cursor-pointer hover:text-gray-700" onclick="sortChannels('last_updated')">
                                最后更新 <i class="fas fa-sort ml-1"></i>
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                状态
                            </th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                操作
                            </th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200" id="channels-table-body">
                        <!-- 频道数据将在这里动态加载 -->
                    </tbody>
                </table>
            </div>
            
            <!-- 加载状态 -->
            <div id="channels-loading" class="hidden">
                <div class="flex items-center justify-center py-12">
                    <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-500"></div>
                    <span class="ml-3 text-gray-600">正在加载频道数据...</span>
                </div>
            </div>
            
            <!-- 空状态 -->
            <div id="channels-empty" class="hidden">
                <div class="text-center py-12">
                    <i class="fas fa-tv text-4xl text-gray-300 mb-4"></i>
                    <h3 class="text-lg font-medium text-gray-900 mb-2">暂无频道数据</h3>
                    <p class="text-gray-500 mb-4">开始添加您的第一个YouTube频道吧</p>
                    <button onclick="openAddChannelModal()" class="inline-flex items-center gap-2 bg-primary-500 text-white px-4 py-2 rounded-button hover:bg-primary-600 transition-colors">
                        <i class="fas fa-plus"></i>
                        添加频道
                    </button>
                </div>
            </div>
            
            <!-- 分页器 -->
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-100">
                <div class="flex items-center justify-between">
                    <div class="text-sm text-gray-700">
                        显示第 <span id="page-start">1</span> - <span id="page-end">10</span> 条，共 <span id="total-count">0</span> 条
                    </div>
                    <div class="flex items-center gap-2">
                        <button id="prev-page" onclick="previousPage()" class="px-3 py-1 text-sm border border-gray-300 rounded hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed">
                            上一页
                        </button>
                        <div class="flex items-center gap-1" id="page-numbers">
                            <!-- 页码将在这里动态生成 -->
                        </div>
                        <button id="next-page" onclick="nextPage()" class="px-3 py-1 text-sm border border-gray-300 rounded hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed">
                            下一页
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 添加频道模态框 -->
    <div id="addChannelModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-[60] hidden flex items-center justify-center px-4">
        <div id="modalContent" class="bg-white rounded-2xl shadow-2xl w-full max-w-3xl transform transition-all duration-300 scale-95 opacity-0">
            <div class="flex items-center justify-between px-6 py-4 border-b border-gray-200">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800">添加频道</h3>
                    <p class="text-sm text-gray-500">输入频道链接，系统会自动预览并补全信息</p>
                </div>
                <button class="p-2 text-gray-500 hover:bg-gray-100 rounded-lg transition" onclick="closeAddChannelModal()">
                    <i class="fas fa-times"></i>
                </button>
            </div>

            <form id="addChannelForm" class="px-6 py-5 space-y-5" onsubmit="return false;">
                <div class="space-y-2">
                    <label for="channel-url" class="block text-sm font-medium text-gray-700">频道 URL</label>
                    <div class="flex gap-3">
                        <input id="channel-url" type="url" class="flex-1 px-4 py-2.5 border border-gray-300 rounded-button focus:outline-none focus:ring-2 focus:ring-primary-200 focus:border-primary-500" placeholder="https://www.youtube.com/@channel" required>
                        <button type="button" class="px-4 py-2.5 rounded-button bg-primary-100 text-primary-700 hover:bg-primary-200 transition" onclick="previewChannel()">
                            <i class="fas fa-eye mr-2"></i>预览
                        </button>
                    </div>
                    <p class="text-xs text-gray-500">支持频道主页或频道 ID 链接</p>
                </div>

                <div class="space-y-2">
                    <label for="channel-name" class="block text-sm font-medium text-gray-700">频道名称 <span class="text-gray-400">(可选)</span></label>
                    <input id="channel-name" type="text" class="w-full px-4 py-2.5 border border-gray-300 rounded-button focus:outline-none focus:ring-2 focus:ring-primary-200 focus:border-primary-500" placeholder="留空将自动根据预览结果填充">
                </div>

                <div class="bg-gradient-to-r from-blue-50 to-indigo-50 rounded-xl p-4 border border-blue-100 space-y-4">
                    <div class="flex items-center gap-3">
                        <div class="w-8 h-8 bg-gradient-to-br from-blue-400 to-blue-600 rounded-lg flex items-center justify-center">
                            <i class="fas fa-dollar-sign text-white text-sm"></i>
                        </div>
                        <div>
                            <h4 class="font-medium text-gray-800">收入设置</h4>
                            <p class="text-xs text-gray-600">选择分类后会自动应用对应的默认 CPM 值</p>
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">频道分类</label>
                            <select id="channel-category" class="w-full px-3 py-2 border border-gray-300 rounded-button focus:border-primary-500 focus:ring-2 focus:ring-primary-200 focus:outline-none transition-all">
                                <option value="">选择分类</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">默认 CPM (每千次展示费用)</label>
                            <div class="relative">
                                <input type="number" id="channel-cpm" class="w-full px-3 py-2 pr-8 border border-gray-300 rounded-button bg-gray-50 focus:border-primary-500 focus:ring-2 focus:ring-primary-200 focus:outline-none transition-all" value="2.50" step="0.01" min="0" readonly>
                                <span class="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 text-sm">$</span>
                            </div>
                            <p id="channel-cpm-hint" class="text-xs text-gray-500 mt-1">默认CPM：$2.50</p>
                        </div>
                    </div>
                </div>

                <div id="channel-preview" class="hidden mb-4 p-4 bg-blue-50 !rounded-button">
                    <h6 class="text-sm font-medium text-blue-900 mb-2">频道预览：</h6>
                    <div id="preview-content" class="text-sm text-gray-600">粘贴频道链接后点击“预览”查看频道详情</div>
                </div>

                <div class="text-xs text-gray-500 bg-blue-50 border border-blue-100 rounded-lg p-3">
                    提示：系统会根据选择的分类自动计算默认 CPM，后续可在“编辑频道”中调整。
                </div>

                <div class="flex items-center justify-end gap-3 border-t border-gray-100 pt-5">
                    <button type="button" class="px-4 py-2.5 rounded-button border border-gray-300 text-gray-600 hover:bg-gray-100 transition" onclick="closeAddChannelModal()">取消</button>
                    <button type="button" class="px-5 py-2.5 rounded-button bg-primary-600 text-white hover:bg-primary-700 transition" onclick="addChannel()">
                        <i class="fas fa-plus mr-2"></i>添加频道
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑频道模态框 -->
    <div id="editChannelModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-[60] hidden flex items-center justify-center px-4">
        <div id="editModalContent" class="bg-white rounded-2xl shadow-2xl w-full max-w-4xl transform transition-all duration-300 scale-95 opacity-0">
            <div class="flex items-center justify-between px-6 py-4 border-b border-gray-200">
                <div>
                    <h3 class="text-xl font-semibold text-gray-800">编辑频道</h3>
                    <p class="text-sm text-gray-500">调整频道基础信息，影响后续数据统计</p>
                </div>
                <button class="p-2 text-gray-500 hover:bg-gray-100 rounded-lg transition" onclick="closeEditChannelModal()">
                    <i class="fas fa-times"></i>
                </button>
            </div>

            <form id="editChannelForm" class="px-6 py-5 space-y-5" onsubmit="return false;">
                <input type="hidden" id="edit-channel-id">

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <div class="space-y-2">
                        <label for="edit-channel-name" class="block text-sm font-medium text-gray-700">频道名称</label>
                        <input id="edit-channel-name" type="text" class="w-full px-4 py-2.5 border border-gray-300 rounded-button focus:outline-none focus:ring-2 focus:ring-primary-200 focus:border-primary-500" placeholder="请输入频道名称" required>
                    </div>

                    <div class="space-y-2">
                        <label for="edit-channel-category" class="block text-sm font-medium text-gray-700">频道分类</label>
                        <select id="edit-channel-category" class="w-full px-4 py-2.5 border border-gray-300 rounded-button focus:outline-none focus:ring-2 focus:ring-primary-200 focus:border-primary-500">
                            <option value="">选择分类</option>
                        </select>
                    </div>

                    <div class="space-y-2">
                        <label for="edit-channel-cpm" class="block text-sm font-medium text-gray-700">CPM 预估（美元）</label>
                        <input id="edit-channel-cpm" type="number" step="0.01" min="0" class="w-full px-4 py-2.5 border border-gray-300 rounded-button bg-gray-50 focus:outline-none focus:ring-2 focus:ring-primary-200 focus:border-primary-500" placeholder="自动根据分类计算" readonly>
                        <p id="edit-channel-cpm-hint" class="text-xs text-gray-500 mt-1">分类默认值：$2.50</p>
                    </div>

                    <div class="space-y-2">
                        <label class="block text-sm font-medium text-gray-700">频道状态</label>
                        <div class="flex items-center gap-3 px-4 py-2.5 border border-gray-300 rounded-button bg-gray-50">
                            <input id="edit-channel-active" type="checkbox" class="h-4 w-4 text-primary-600 border-gray-300 rounded">
                            <span class="text-sm text-gray-700">活跃状态</span>
                        </div>
                    </div>
                </div>

                <div class="bg-gray-50 border border-gray-200 rounded-xl p-4">
                    <h4 class="text-sm font-semibold text-gray-700 mb-3">最新数据概览</h4>
                    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 text-center">
                        <div class="p-3 bg-white rounded-lg border">
                            <div class="text-lg font-semibold text-primary-600" id="edit-stats-subscribers">-</div>
                            <div class="text-xs text-gray-500">订阅数</div>
                        </div>
                        <div class="p-3 bg-white rounded-lg border">
                            <div class="text-lg font-semibold text-green-600" id="edit-stats-views">-</div>
                            <div class="text-xs text-gray-500">总播放量</div>
                        </div>
                        <div class="p-3 bg-white rounded-lg border">
                            <div class="text-lg font-semibold text-purple-600" id="edit-stats-videos">-</div>
                            <div class="text-xs text-gray-500">视频数</div>
                        </div>
                        <div class="p-3 bg-white rounded-lg border">
                            <div class="text-sm font-semibold text-gray-700" id="edit-stats-updated">-</div>
                            <div class="text-xs text-gray-500">最近同步</div>
                        </div>
                    </div>
                </div>

                <div class="flex items-center justify-end gap-3 border-t border-gray-100 pt-5">
                    <button type="button" class="px-4 py-2.5 rounded-button border border-gray-300 text-gray-600 hover:bg-gray-100 transition" onclick="closeEditChannelModal()">取消</button>
                    <button type="button" class="px-5 py-2.5 rounded-button bg-primary-600 text-white hover:bg-primary-700 transition" onclick="updateChannel()">
                        <i class="fas fa-save mr-2"></i>保存修改
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- JavaScript -->
    <script src="js/env.js"></script>
    <script>
    // 设置全局API配置
    window.API_BASE_URL = API_BASE_URL;
    </script>
    <script src="js/config.js"></script>
    <script src="js/utils.js"></script>
    <script src="js/api.js"></script>
    <script src="js/channel-management.js"></script>
    <script src="js/shared-navigation.js"></script>
    <script>
    // 页面加载完成后初始化频道管理
    document.addEventListener('DOMContentLoaded', function() {
        initChannelManagement();
    });
    </script>
</body>
</html>
